package com.qfedu.defect.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qfedu.defect.task.dto.DefectTaskDto;
import com.qfedu.defect.task.mapper.DefectTaskMapper;
import com.qfedu.defect.task.pojo.DefectTask;
import com.qfedu.defect.task.service.DefectTaskService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

@Service
public class DefectTaskServiceImpl extends ServiceImpl<DefectTaskMapper, DefectTask> implements DefectTaskService {

    @Transactional(readOnly = true)
    @Override
    public Page<DefectTask> getDefectTasks(DefectTaskDto taskDto) {
        LambdaQueryWrapper<DefectTask> wrapper = new LambdaQueryWrapper<>();
        String name = taskDto.getName();
        wrapper.like(StringUtils.hasLength(name), DefectTask::getName, name);
        Integer state = taskDto.getState();
        wrapper.eq(state != null, DefectTask::getState, state);
        String defectUser = taskDto.getDefectUser();
        wrapper.eq(StringUtils.hasLength(defectUser), DefectTask::getDefectUser, defectUser);

        Page<DefectTask> page = new Page<>(taskDto.getCurrentPage(), taskDto.getPageSize());
        return page(page, wrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean save(DefectTask defectTask) {
        String defectUser = defectTask.getDefectUser();
        defectTask.setState(StringUtils.hasLength(defectUser) ? 1 : 0);
        return super.save(defectTask);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean updateById(DefectTask defectTask) {
        String defectUser = defectTask.getDefectUser();
        defectTask.setState(StringUtils.hasLength(defectUser) ? 1 : 0);
        return super.updateById(defectTask);
    }
}
